<?php

namespace App\Controller;
/**
 * 基础控制器
 *
 * @author WangBin
 */
class BaseController
{
    const MESSAGE_SUCCESS = '处理成功';
    const MESSAGE_FAIL = '处理失败';

    private function returnData(int $code, string $message, $data = null)
    {
        $result = ['code' => $code, 'message' => $message];
        if(!is_null($data)) {
            $result['data'] = $data;
        }
        return $result;
    }

    protected function success($data = null)
    {
        return $this->returnData(200, self::MESSAGE_SUCCESS, $data);
    }

    protected function error(int $code = 500, string $message = '')
    {
        $messageStr = $message ?? self::MESSAGE_FAIL;
        return $this->returnData($code, $messageStr);
    }
}